package cn.jinjimi.app.main.activity;

import android.text.Editable;
import android.text.Selection;
import android.widget.EditText;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.UploadProfileEvent;
import cn.jinjimi.app.entity.Account;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by haner on 15-1-26.
 */
public class PersonalDescActivity extends ToolBarCommonActivity {

    private EditText personalDesc;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_personaldesc);
        setTitle(getString(R.string.info_desc));
    }

    @Override
    protected void initial() {
        initView();
    }

    protected void initView() {
        personalDesc = (EditText) findViewById(R.id.et_personal_desc);
        setRightText(R.string.save);

        //光标移到句末
        Editable etext = personalDesc.getText();
        Selection.setSelection(etext, etext.length());

    }

    @Override
    protected void rightAction() {
        super.rightAction();
        savePersonalDesc();
    }

    /* 保存个人简介 */
    private void savePersonalDesc() {
        showLoadingIndicator();
        UserController.setPersonalDesc(personalDesc.getText().toString().trim());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUploadProfileEvent(UploadProfileEvent event) {
        if (isFinishing())
            return;
        switch (event.state) {
            case DataEvent.SUCC:
                ToastUtils.toastMessage(PersonalDescActivity.this, getString(R.string.modify_success));
                finish();
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(PersonalDescActivity.this, getString(R.string.modify_fail));
                break;
        }
        hideLoadingIndicator();
    }
}
